/**
 * 128. 最长连续序列
 * https://leetcode-cn.com/problems/longest-consecutive-sequence/
 */

/**
 * @param {number[]} nums
 * @return {number}
 */
function longestConsecutive(nums) {
  const set = new Set(nums);
  let max = 0;
  for (let i = 0; i < nums.length; i += 1) {
    if (!set.has(nums[i] - 1)) {
      let length = 1;
      let cur = nums[i] + 1;
      while (set.has(cur)) {
        length += 1;
        cur += 1;
      }
      if (length > max) {
        max = length;
      }
    }
  }
  return max;
}

console.log(longestConsecutive([100, 4, 200, 1, 3, 2]) === 4);
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]) === 9);
